package ie.dkit.java3Demulation.drawer;

import ie.dkit.java3Demulation.objects.Circle;
import ie.dkit.java3Demulation.objects.Point;

import java.awt.Graphics;

public class CircleDrawer extends Drawer {

	public CircleDrawer(Graphics graphics, int xMiddle, int ymiddle) {
		super(graphics, xMiddle, ymiddle);
	}

	public void draw(Circle circle) {
		drawCircleAlgorithm(circle.getCenter(), circle.getRadius());
	}

	protected void drawCircleAlgorithm(Point c, double r) {
		Point p = new Point(c.getX() + r, c.getY());
		int xc = (int) c.getX();
		int yc = (int) c.getY();
		int y = (int) p.getY();
		for (int x = (int) p.getX(); x >= xc; x--) {
			int distXY = (int) Math.sqrt(Math.pow(x - xc, 2)
					+ Math.pow(y - yc, 2));
			writePixel(x, y);
			writePixel(-x + (2 * xc), y);
			writePixel(x, -y + (2 * yc));
			writePixel(-x + (2 * xc), -y + (2 * yc));
			while (distXY < r) {
				writePixel(x, y);
				writePixel(-x + (2 * xc), y);
				writePixel(x, -y + (2 * yc));
				writePixel(-x + (2 * xc), -y + (2 * yc));
				y += 1;
				distXY = (int) Math.sqrt(Math.pow(x - xc, 2)
						+ Math.pow(y - yc, 2));
			}
		}
	}

}
